using Identity.Application.Contracts.Common;
using Identity.Application.Permissions.Interfaces;
using Identity.Application.Permissions.Queries;
using MediatR;

namespace Identity.Application.Permissions.QueryHandlers
{
    public class GetPermissionByIdQueryHandler : IRequestHandler<GetPermissionByIdQuery, PermissionDto>
    {
        private readonly IPermissionsReadService _permissionsReadService;
        public GetPermissionByIdQueryHandler(IPermissionsReadService permissionsReadService)
        {
            _permissionsReadService = permissionsReadService;
        }
        public async Task<PermissionDto> Handle(GetPermissionByIdQuery request, CancellationToken cancellationToken)
        {
            var permission = await _permissionsReadService.GetByIdAsync(request.Id);
            if (permission == null) throw new KeyNotFoundException($"无法找到permissionId:{request.Id} 的权限记录");

            return permission;
        }
    }
}